﻿var userslist = window.userslist || {};
userslist = (function () {
    var processAll = function () {
        var dq = dashingquill;
        var you = dq.find('#you');

        if (you) {
            dq.bind(you, 'click', function () {
                location = dq.domainName + '/' + you.getAttribute('accesskey');
            });
        }


        dq.bind('.followers, .following', 'click', function (user) {

            var classes = dq.find('.members-list-visible');
            dq.each(classes, function (c) {
                var usersList = dq.find('.users-list', c.parentNode);
                if (usersList) {
                    c.className = 'members';
                    dq.hide(usersList);
                }
            });

            var usersList = dq.find('.users-list', user.parentNode)[0];

            var followingOrFollowers = '';
            if (user.className == 'following') {
                followingOrFollowers = 'following';
            }
            else {
                followingOrFollowers = 'followers';
            }

            dq.xhr({
                url: dq.domainName + '/GetFollowingOrFollowers?userToken=' + user.parentNode.parentNode.id + '&followingOrFollowers=' + followingOrFollowers,
                callback: function (response) {
                    if (response.IsSuccess == true) {
                        if (response.Members == "No members yet!") {
                            alert(response.Members);
                            return;
                        }
                        var splittedNames = response.Members.split(',');
                        var members = '';

                        dq.each(splittedNames, function (name, i) {
                            members += '<div>' + name + '</div>';
                        });

                        usersList.innerHTML = members;
                        dq.show(usersList);
                        user.className = 'members-list-visible';
                    }
                    else alert(response.Error);
                }
            });
        });

        dq.bind(document, 'click', function () {
            var classes = dq.find('.members-list-visible');
            dq.each(classes, function (c) {
                var usersList = dq.find('.users-list', c.parentNode);
                if (!usersList || usersList.innerHTML == '') {
                    c.className = 'members';
                } else {
                    if (c.getAttribute('accesskey') == 'following') {
                        c.className = 'following';
                    } else {
                        c.className = 'followers';
                    }
                    dq.hide(usersList);
                }
            });
        });
    };
    return {
        processAll: function () {
            return processAll();
        }
    };
})(userslist);